package sm.pzavoli.mercato.filtro;

public class ButterFiltro {
	double a0[] = new double[2];
	double a1[] = new double[2];
	double a2[] = new double[2];
	double b1[] = new double[2];
	double b2[] = new double[2];
		
	public ButterFiltro() {
		
	}
	public void setParams(int biquad, double a0, double a1, double a2, double b1, double b2) {
		this.a0[biquad] = a0;
		this.a1[biquad] = a1;
		this.a2[biquad] = a2;
		this.b1[biquad] = b1;
		this.b2[biquad] = b2;
	}
	/*
	public boolean transform(double x[], double y[]) {
		try {
			// Primo biquad
			double yt[] = new double [x.length + 2];
			double yt0 = a0[0] * x[0];
			double yt1 = a0[0] * x[1] + a1[0] * x[0] - b1[0] * yt0 ;
			double y0 = a0[1] * yt0 ;
			double y1 = a0[1] * yt1 + a1[1] * yt0 - b1[1] * y0;
			for ( int i = 2; i < x.length; i++) {
				yt[i] = a0[0] * x[i] + a1[0] * x[i - 1] + a2[0] * x[i - 2] - b1[0] * yt[i - 1] - b2[0] * yt[i -2];
				y[i] = a0[1] * yt[i] + a1[1] * yt[i - 1] + a2[1] * yt[i - 2] - b1[1] * y[i - 1] - b2[1] * y[i -2];
			}
			System.arraycopy(arg0, arg1, arg2, arg3, arg4)
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		}
	}*/
	public boolean transform(float x[], double y[]) {
		try {
			// Primo biquad
			double yt[] = new double [x.length];
			yt[0] = a0[0] * x[0];
			yt[1] = a0[0] * x[1] + a1[0] * x[0] - b1[0] * yt[0] ;
			y[0] = a0[1] * yt[0] ;
			y[1] = a0[1] * yt[1] + a1[1] * yt[0] - b1[1] * y[0];
			for ( int i = 2; i < x.length; i++) {
				yt[i] = a0[0] * x[i] + a1[0] * x[i - 1] + a2[0] * x[i - 2] - b1[0] * yt[i - 1] - b2[0] * yt[i -2];
				y[i] = a0[1] * yt[i] + a1[1] * yt[i - 1] + a2[1] * yt[i - 2] - b1[1] * y[i - 1] - b2[1] * y[i -2];
			}
			return true;
		} catch(Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
}
